package com.example.hospitalManage.controller.admin.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hospitalManage.entity.OrderOrderInfo;
import com.example.hospitalManage.service.OrderOrderInfoService;
import com.example.hospitalManage.utils.Response;
import com.example.hospitalManage.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @projectName: hospitalManage
 * @package: com.example.hospitalManage.api.admin
 * @className: OrderOrderInfoController
 * @author: GCT
 * @description: TODO
 * @date: 2022/12/17 18:35
 * @version: 1.0
 */
@RestController //=@ResponseBody+@Controller
@RequestMapping(value = "/admin")
@Api(tags = "订单表操作相关接口")
@CrossOrigin
public class OrderOrderInfoController {

//    Todo:逻辑删除处理
//    Todo：简化数据传输实体类

    //定义日志对象
    public final static Logger log =
            LoggerFactory.getLogger(OrderOrderInfoController.class);


    @Autowired
    OrderOrderInfoService orderOrderInfoService;


    //GET请求：查询订单列表
//    @RequiresPermissions("user:list")
    @GetMapping(value = "/orderOrderInfo")
    @ApiOperation("查询所有订单信息的接口")
    public ResponseResult<List<OrderOrderInfo>> getOrderList(){
        List<OrderOrderInfo> orderList = orderOrderInfoService.list();
        System.out.println(orderList);
        log.info("===================");
        return Response.createOkResp(orderList);
    }


    @PostMapping(value ="/orderOrderInfo/{current}/{limit}")
    @ApiOperation("查询订单分页")
    public ResponseResult getAllOrderListByPage(@PathVariable long current,
                                                @PathVariable long limit,
                                                @RequestBody(required = false) OrderOrderInfo orderOrderInfo){
        //创建page对象，传递当前页，每页记录数
        Page<OrderOrderInfo> page = new Page<>(current, limit);
        QueryWrapper<OrderOrderInfo> wrapper = new QueryWrapper<>();
        String hosname = orderOrderInfo.getHosname();
        String outTradeNo = orderOrderInfo.getOutTradeNo();
        String patientName = orderOrderInfo.getPatientName();
        Date createTime = orderOrderInfo.getCreateTime();
        Date reserveDate = orderOrderInfo.getReserveDate();
        Integer orderStatus = orderOrderInfo.getOrderStatus();
        if (!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname", hosname);
        }
        if (!StringUtils.isEmpty(outTradeNo)) {
            wrapper.like("out_trade_no", outTradeNo);
        }
        if (!StringUtils.isEmpty(patientName)) {
            wrapper.eq("patient_name", patientName);
        }
        if (!StringUtils.isEmpty(createTime)) {
            wrapper.like("create_time", createTime);
        }
        if (!StringUtils.isEmpty(reserveDate)) {
            wrapper.eq("reserve_date", reserveDate);
        }
        if (!StringUtils.isEmpty(orderStatus)) {
            wrapper.eq("order_status", orderStatus);
        }

        IPage<OrderOrderInfo> pageOrderOrderInfo = orderOrderInfoService.page(page, wrapper);
        return Response.createOkResp(pageOrderOrderInfo);
    }

    @GetMapping(value = "/orderOrderInfo/getById/{id}")
    @ApiOperation("根据父id查询对应值列表")
    public ResponseResult<OrderOrderInfo> getDictByParentId(@PathVariable String id){
        QueryWrapper<OrderOrderInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        OrderOrderInfo query = orderOrderInfoService.getOne(wrapper);
        return Response.createOkResp(query);
    }


    //GET请求：根据订单相关信息动态查询订单详情
//    @RequiresPermissions("user:list")
    @GetMapping(value = "/orderOrderInfoDynm")
    @ApiOperation("根据传入的订单信息查询对应订单信息的接口")
    public ResponseResult<List<OrderOrderInfo>> getOrderListDynm(OrderOrderInfo orderOrderInfo){
        QueryWrapper<OrderOrderInfo> objectQueryWrapper = new QueryWrapper<>(orderOrderInfo);
        List<OrderOrderInfo> orderList = orderOrderInfoService.list(objectQueryWrapper);
        System.out.println(orderList);
        log.info("===================");
        return Response.createOkResp(orderList);
    }

//    POST请求，处理页面表单中提交的数据，向数据库增加数据
//    @RequiresPermissions("user:create")
    @PostMapping(value = "/orderOrderInfo")
    @ApiOperation("新增订单信息的接口")
    public ResponseResult<OrderOrderInfo> addOrder(OrderOrderInfo orderOrderInfo){
        boolean save = orderOrderInfoService.save(orderOrderInfo);
        System.out.println(save);
        return Response.createOkResp("添加成功",orderOrderInfo);
    }

    // TODO: 2022/12/17 :根据订单交易号更新？
    //PUT请求：根据传入的订单id更新对应订单的对应信息
//    @RequiresPermissions("user:update")
    @PutMapping(value = "/orderOrderInfo")
    @ApiOperation("根据订单id更新订单信息的接口")
    public ResponseResult<OrderOrderInfo> updateStudent(OrderOrderInfo orderOrderInfo){
        System.out.println("order: ");
        System.out.println(orderOrderInfo);

        QueryWrapper<OrderOrderInfo> objectQueryWrapper = new QueryWrapper<>(orderOrderInfo);
//        objectQueryWrapper.eq("out_trade_no",orderOrderInfo.getOutTradeNo());
//        boolean update = orderOrderInfoService.update(objectQueryWrapper);
        boolean update = orderOrderInfoService.updateById(orderOrderInfo);
        System.out.println(update);
        OrderOrderInfo orderOrderInfoUpdate = orderOrderInfoService.getOne(objectQueryWrapper);
//        return Response.createOkResp(student);
//        返回修改后的学生信息
        return Response.createOkResp("更新成功",orderOrderInfoUpdate);
    }

    //DELETE请求：根据传入的订单id逻辑删除对应订单的对应信息
//    @RequiresPermissions("user:delete")
    @DeleteMapping(value = "/orderOrderInfo/{id}")
    @ApiOperation("根据订单id删除订单信息的接口")
    public ResponseResult<Boolean> deleteStudent(@PathVariable("id") Long id){
        System.out.println("id: ");
        System.out.println(id);

        boolean res = orderOrderInfoService.removeById(id);

//        返回修改后的学生信息
        return Response.createOkResp("逻辑删除成功",res);
    }

    //DELETE请求：根据传入的订单id数组批量逻辑删除对应订单的对应信息
//    @RequiresPermissions("user:delete")
    @DeleteMapping(value = "/orderOrderInfoBatch")
    @ApiOperation("根据订单id删除订单信息的接口")
    public ResponseResult<Boolean> deleteStudentBatch(@RequestBody Long[] ids){
        System.out.println("ids: ");
        System.out.println(ids);

        List<Long> idList = new ArrayList<>();
        for (Long id : ids){
            idList.add(id);
        }
        boolean res = orderOrderInfoService.removeByIds(idList);

//        返回修改后的学生信息
        return Response.createOkResp("批量逻辑删除成功",res);
    }






}
